package org.aicfve.previze.controller;

import com.hava.web.rest.Entity;
import org.aicfve.config.StorageConfig;
import org.aicfve.previze.entity.Scene;
import org.aicfve.previze.service.SceneJPAService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping(value="rest/scenes/{id}/previews")
public class ScenePreviewRestController {

    @Autowired
    SimpleDateFormat timeFormat;

    @Resource
    private StorageConfig storageConfig;

    @Autowired
    private SceneJPAService entityService;

    @RequestMapping(value = "/lastest",method = RequestMethod.GET)
    public void getLastestPreview(@PathVariable("id") Long id){
        // todo:get last preview
    }

    @RequestMapping(method = RequestMethod.GET)
    public void getPreviews(@PathVariable("id") Long id) {
        // todo: get Last Preview
    }

    @RequestMapping(value = "/{version}",method = RequestMethod.GET)
    public void getVersionPreview(@PathVariable("id") Long id,@PathVariable("version") String version) {
        // todo: get Version Preview
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity uploadPreview(@PathVariable("id") Long id, @RequestParam("file")CommonsMultipartFile file,UriComponentsBuilder uriBuilder) throws IOException {

        HttpHeaders httpheader = new HttpHeaders();
        ResponseEntity responseEntity;
        //
        Scene entity = entityService.get(id);
        if(entity == null) {
            responseEntity = new ResponseEntity(httpheader,HttpStatus.NO_CONTENT);
            return responseEntity;
        }

        // done: file storage where
        String save_path= storageConfig.getSavepath() + timeFormat.format(new Date()) + "_" + file.getOriginalFilename();

        // save file
        File saveFile = new File(save_path);
        System.out.println("[file.path]:" + saveFile.getPath());
        file.transferTo(saveFile);

        // create new snapshot

        // todo: return new version url
        //通过反射获取entity的url
        String EntityURL = Entity.URL(this) + "/" + "v1";//snapshot.getVersion();
        //生成Respone header显示Entity创建后的资源路径
        URI uri = uriBuilder.path(EntityURL).build().toUri();
        httpheader.setLocation(uri);
        responseEntity = new ResponseEntity(httpheader, HttpStatus.CREATED);

        return responseEntity;
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void delete(@PathVariable("id") Long id,@PathVariable("version") String version) {
        //todo:delete preview version
    }
}
